import { createRouter, createWebHistory } from "vue-router";

import LayoutView from "../views/layout/LayoutView.vue";
import HomeView from "../views/home/HomeView.vue";
import TeamManage from "../views/resource/Team/TeamManage.vue";
import addTeam from "../views/resource/Team/addTeam.vue";
import TeamDetail from "../views/resource/Team/TeamDetail.vue";
import ResidentView from "../views/resident/ResidentView.vue"
import ResidentAdd from "../views/resident/info/ResidentAdd.vue";
import LoginView from "../views/login/loginView.vue";
import DoctorManage from "../views/resource/Doctor/DoctorManage.vue";
import { useGlobalStore } from "../store/global";
import AddDoctor from "../views/resource/Doctor/addDoctor.vue";
import DoctorDetail from "../views/resource/Doctor/DoctorDetail.vue";
import AgencyManage from "../views/resource/Agency/AgencyManage.vue";
import AddAgency from "../views/resource/Agency/addAgency.vue";
import TagControl from "../views/settings/tagcontrol/TagControl.vue";
import RoleMange from "../views/settings/role/roleMange.vue";
import ServicePacks from "../views/data/servicePacks/servicePacks.vue";
import AddServicePackage from "../views/data/servicePacks/addServicePackage.vue";
import ResidentInfo from "../views/resident/info/ResidentInfo.vue";
import ResidentUpdate from "../views/resident/info/ResidentUpdate.vue";
import ServiceProjectManage from "../views/data/serviceProject/serviceProjectManage.vue";
import AddServiceProjectManage from "../views/data/serviceProject/addServiceProjectManage.vue";

// import Pending from "../views/CoManagement/Pending/pending.vue";//待处理签约
import SearchSon from "../views/CoManagement/pending/SearchSon.vue";//签约处理详情
import Redact from "../views/CoManagement/pending/redact.vue";//签约处理编辑

import Record from "../views/CoManagement/Record/record.vue";//签约记录
import Recorddetails from "../views/CoManagement/Record/Recorddetails.vue";//签约记录签约详情
import Redactdetails from "../views/CoManagement/Record/redactdetails.vue";//签约记录编辑



// import Pendings from "../views/serve/pendings/pendings.vue";//待处理服务
import Records from "../views/serve/records/records.vue";//编辑服务处理
import Searchsons from "../views/serve/pendings/searchsons.vue";//服务详情


import Redacts from "../views/serve/pendings/redacts.vue";//服务记录
import Servicedetails from "../views/serve/records/Servicedetails.vue";//服务记录详情页
import Pending from "../views/CoManagement/Pending/pending.vue";
import Pendings from "../views/serve/pendings/pendings.vue";


const routes = [
    // 重定向
    { path: '/', redirect: '/login' },
    {
        // 布局页的路由
        path: '/',
        component: LayoutView,
        name: 'LayoutView',
        redirect: '/home',
        children: [
            {
                //团队管理
                path: '/data/team/list',
                component: TeamManage,
            },
            {
                //主页
                path: '/home',
                component: HomeView,
            },



            {
                path: '/sign/pending', //待处理签约路由
                component: Pending,
            },
            {
                path: '/admin/CoManagement/SearchSon',//待处理查看详情跳转路由
                component: SearchSon,
            },
            {
                path: '/admin/CoManagement/Redact',//待处理编辑页面跳转路由
                component: Redact,
            },




            {
                path: '/sign/record',//签约记录路由
                component: Record,
            },
            {
                path: '/admin/CoManagement/Recorddetails',//签约记录详情跳转路由
                component: Recorddetails,
            },
            {
                path: '/admin/CoManagement/Redactdetails',//签约记录续约跳转路由
                component: Redactdetails,
            },




            {
                path: '/service/pending', //待处理服务路由
                component: Pendings,
            },

            {
                path: '/admin/serve/Searchsons',//查看服务详情跳转路由
                component: Searchsons,
            },
            {
                path: '/admin/serve/Redacts',//编辑服务页面跳转路由
                component: Redacts,
            },



            {
                path: '/service/record',//服务记录路由
                component: Records,
            },
            {
                path: '/admin/serve/Servicedetails',//查看服务记录详情页面跳转路由
                component: Servicedetails,
            },
            {
                //居民管理
                path: '/resident/list',
                component: ResidentView,
            },
            {
                //新增居民档案
                path: '/resident/ResidentAdd',
                component: ResidentAdd
            },
            {
                //编辑居民档案
                path: '/resident/update/:_id',
                component: ResidentUpdate
            },
            {
                //居民详情
                path: '/resident/detail/:_id',
                component: ResidentInfo
            },
            {
                //标签管理
                path: '/setting/tag/list',
                component: TagControl,
            },
            {
                //服务包管理
                path: '/data/servicePack/list',
                component: ServicePacks,
            },
            {
                //角色管理
                path: '/setting/role/list',
                component: RoleMange,
            },
            {
                //新增团队
                path: '/data/team/add',
                component: addTeam,
            },
            {
                //团队详情
                path: '/data/team/detail/:_id',
                component: TeamDetail,
            },
            {
                //添加服务包
                path: '/data/servicePack/add',
                component: AddServicePackage,
            },
            {
                //医生管理
                path: '/data/doctor/list',
                component: DoctorManage
            },
            {
                //新增医生
                path: '/data/doctor/add',
                component: AddDoctor
            },
            {
                //医生详情
                path: '/data/doctor/detail/:_id',
                component: DoctorDetail
            },
            {
                //机构管理
                path: '/data/organization/list',
                component: AgencyManage
            },
            {
                //新增机构
                path: '/data/organization/add',
                component: AddAgency
            },
            {
                //服务项目管理
                path: '/data/serviceItem/list',
                component: ServiceProjectManage
            },
            {
                //新增服务项目
                path: '/data/serviceItem/add',
                component: AddServiceProjectManage
            },
        ]
    },
    {
        path: '/login', component: LoginView
    }

    // { path: '/:pathMatch(.*)*', component: NotFound }
]

const router = createRouter({
    // 设置路由模式（history模式）
    history: createWebHistory(),
    // 配置路由对象（设置路径URL和组件之间的关联关系）
    routes: routes
})

//不需要登录可以访问
const WHITE_LIST = ['/login']
//全局前置守卫
router.beforeEach(async (to) => {
    const token = localStorage.doctor_token;

    if (token && to.path == '/login') {
        return '/home';//直接跳转到主页
    }

    if (!WHITE_LIST.includes(to.path)) {
        if (!token) {
            return '/login'// 没有登录不允许访问白名单以外的页面
        }
    }
    //获取仓库对象
    const store = useGlobalStore();
    //调用仓库方法
    store.getDoctorInfoByTokenAction()
})

export function registerRouter(app) {
    app.use(router);
}
